﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfApp4DataGrid.Models;

namespace WpfApp4DataGrid.Db
{
    public class localDb
    {
        public localDb()
        {
            Init();
        }

        private List<Student> Students;

        private void Init()
        {
            Students = new List<Student>();
            for(int i = 0; i < 30; i++)
            {
                Students.Add(new Models.Student()
                {
                    IsCheck = true,
                    Id = i,
                    Name = $"Sample{i}"
                });
            }
        }

        public List<Student> GetStudents()
        {
            return Students;
        }

        public void AddStudent(Student stu)
        {
            Students.Add(stu);
        }
        public void AddStudentAtHead(Student stu)
        {
            Students.Insert(0, stu);
        }

        public void DelStudent(int id)
        {
            var model = Students.FirstOrDefault(t => t.Id == id);
            if (model != null)
            {
                Students.Remove(model);
            }
        }
        // 全选
        public void checkAllStudent(Boolean status)
        {
            Students.ForEach(item=> item.IsCheck = status);
        }

        public List<Student> getStudentByName(string name)
        {
            return Students.Where(q => q.Name.Contains(name)).ToList();
        }
        public Student GetStudentById(int id)
        {
            var model = Students.FirstOrDefault(t => t.Id == id);
            // 做一次深拷贝
            if(model != null)
            {
                return new Student()
                {
                    Id = model.Id,
                    Name = model.Name
                };
            }
            return null;
        }

    }
}
